package org.hisp.dhis.wsclient.j2me.javarosa;

import java.util.Hashtable;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;

import org.javarosa.core.Context;
import org.javarosa.core.api.Constants;
import org.javarosa.core.api.IActivity;
import org.javarosa.core.api.IShell;

import de.enough.polish.ui.Container;
//import de.enough.polish.ui.Form;
import de.enough.polish.ui.ImageItem;
import de.enough.polish.ui.Item;
import de.enough.polish.ui.ItemCommandListener;
import de.enough.polish.ui.StringItem;
import de.enough.polish.ui.FilteredList;
import de.enough.polish.ui.List;

public class OrgUnitActivity 
implements IActivity, CommandListener, ItemCommandListener{

	//Arraylist orgUnits;
	
    public static final String CMD_KEY = "command";
    public static final String CMD_EXIT = "exit";
    //public static final String CMD_ = "add/update dataset";
    //public static final String CMD_DATASET_SIMPLIFIED_ADDUPDATE = "simplified add/update dataset";

    private IShell shell;

    //private Form form;
	
	private FilteredList fList;
	
    private Command cmdExit;
    private Command cmdOk;

    //private Container menu;
    
	//private StringItem miAddUpdateDataset;
    //private StringItem miSimplifiedAddUpdateDatasetSpike;
	
	//private StringItem miEnterData;
    //private StringItem miBrowseDHIS;

    public OrgUnitActivity( IShell shell )
    {
        setShell( shell );
        createWelcomeForm();
    }

    public void contextChanged( Context globalContext )
    {
        // TODO Auto-generated method stub

    }

    public void destroy()
    {
        // TODO Auto-generated method stub

    }

    public Context getActivityContext()
    {
        // TODO Auto-generated method stub
        return null;
    }

    public void halt()
    {
        // TODO Auto-generated method stub

    }

    public void resume( Context globalContext )
    {
        shell.setDisplay( this, Util.createIView( fList ) );
    }

    public void setShell( IShell shell )
    {
        this.shell = shell;
    }

    public void start( Context context )
    {
        shell.setDisplay( this, Util.createIView( fList ) );
    }

    private void createWelcomeForm()
    {
        //#style .filteredListItem, .filteredListItem, .filteredTextFieldStyle
		fList = new FilteredList( "Select OrgUnit", List.IMPLICIT );
		
		//#style .filteredListItem, .filteredListItem, .filteredTextFieldStyle
		fList.append("A State", null);
		fList.append(new RESTClient().getResult(), null);
		fList.append("State C", null);
		fList.append("B State", null);
		fList.append("C State", null);
		fList.append("D State", null);
		fList.append("E State", null);
		fList.append("F State", null);
		fList.append("D State", null);
		fList.append("GJ State", null);
		fList.append("AWS State", null);
		fList.append("A ewState", null);
		fList.append("A Steedate", null);
		fList.append("w Staterg", null);
		fList.append("A Stargte", null);

		fList.append("A State", null);
		fList.append("Setate C", null);
		fList.append("Bef State", null);
		fList.append("C dState", null);
		fList.append("D fdState", null);
		fList.append("E Sdfstate", null);
		fList.append("gF State", null);
		fList.append("dD State", null);
		fList.append("dfgGJ State", null);
		fList.append("dgfAWS State", null);
		fList.append("dA ewState", null);
		fList.append("ffgbA Steedate", null);
		fList.append("w Sdtaterg", null);
		fList.append("Adf Stargte", null);			
		
		/*         for ( Enumeration e = orgUnits.elements(); e.hasMoreElements(); )
        {
            fList.append( ((OrganisationUnit) e.nextElement()).getName(), null );
        } */
    					

		// General DHIS Activity materials ends
		

		
        // Add form-commands
        cmdExit = new Command( "Exit", Command.EXIT, 1 );
        fList.addCommand( cmdExit );

        // Add menu items with commands

        cmdOk = new Command( "Start activity", Command.OK, 0 );

        fList.setCommandListener( this );
    }

    private StringItem createMenuItem( String text, Command cmd )
    {
        //#style .welcomeMenuItem
        StringItem item = new StringItem( null, text );
        item.setDefaultCommand( cmd );
        item.setItemCommandListener( this );
        return item;
    }

    public void commandAction( Command cmd, Displayable disp )
    {
        System.out.println( "Cmd: " + cmd.getLabel() );

        if ( cmd == cmdExit )
        {
            Hashtable returnArgs = new Hashtable();
            returnArgs.put( CMD_KEY, CMD_EXIT );
            shell.returnFromActivity( this, Constants.ACTIVITY_COMPLETE, returnArgs );
        }
    }

    public void commandAction( Command cmd, Item item )
    {
        System.out.println( "Cmd: " + cmd.getLabel() + ", Item: " + item.toString() );

/*         if ( cmd == cmdOk )
        {
            Hashtable returnArgs = new Hashtable();

            if ( item == miEnterData )
            {
                
				
				/* returnArgs.put( CMD_KEY, CMD_DATASET_ADDUPDATE );
                shell.returnFromActivity( this, Constants.ACTIVITY_NEEDS_RESOLUTION, returnArgs ); */
     /*        }
            else if ( item == miBrowseDHIS )
            {
                /* returnArgs.put( CMD_KEY, CMD_DATASET_SIMPLIFIED_ADDUPDATE );
                shell.returnFromActivity( this, Constants.ACTIVITY_NEEDS_RESOLUTION, returnArgs ); */
       //     }
      //  }  
    }

}
